$ ()->

    ids = []
    $('.location-status').each ()->
        ids.push $(@).data('id')

    loadStatus ids

loadStatus = (ids)->
    els = $ '.location-status'
    loading = '<img src="/img/loading.gif">'
    
    request = $.ajax
      url: '/locations/get_status/' + ids.join('/')
      beforeSend: ()->
        els.prepend loading
      complete: (req)->
        html = ''
        els.empty()
        if req.status == 200
            data = eval '('+req.responseText+')'
            for id of data
              locationStatus id, data[id]
        else
          alert req.status+'.'+req.statusText

locationStatus = (id, data)->
    span = $ '#location-status-'+id

    clientLink = (data, before, after)->
      before = '' unless before
      after  = '' unless after

      return before + ($('<a>')
        .text(data.data.Client.name)
        .attr('href', '/clients/edit/'+data.data.Client.id)).prop('outerHTML') + after

    switch data.status.toString().toLowerCase()
      when 'free'
          span
              .attr 'title', 'Slobodno'
              .addClass 'free'

      when 'occupied'
          span
              .attr 'title', 'Zauzeto'
              .addClass 'occupied'
              .after clientLink(data, " (", ")")
              .after(
                  $('<a>')
                      .text data.data.Campaign.name
                      .attr 'href', '/campaigns/edit/'+data.data.Campaign.id
              )

      when 'reserved'
          LocRes = data.data.LocationReservation
          span
              .attr 'title', 'Rezervisano'
              .addClass 'reserved'
              .after(
                  " (" + (
                      $('<a>')
                          .text LocRes.start+" / "+LocRes.end
                          .attr 'href', '/location_reservations/edit/'+LocRes.id
                  ).prop('outerHTML') + ")"
              )
              .after clientLink(data)
    